﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace SEP.Admin
{
    public partial class ServiceReportJob : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                //postback
                string target = Request.Form["__EVENTTARGET"];
                string argument = Request.Form["__EVENTARGUMENT"];

                if (target.Equals(updatePanelJobs.ClientID))
                {
                    BindData();
                }
            }
            else
            {
                //not a postback
                BindData();
            }
        }

        private void BindData()
        {
            grvJobs.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
            grvJobs.DataSource = Job.GetAllUnarchived();
            grvJobs.DataBind();

            if (ViewState["PAGEINDEX"] != null)
            {
                int pageIndex = 0;

                if (int.TryParse(ViewState["PAGEINDEX"].ToString(), out pageIndex))
                {
                    grvJobs.PageIndex = pageIndex;
                }
            }
        }

        protected void grvJobs_OnSelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = grvJobs.DataKeys[e.NewSelectedIndex].Value.ToString();

            Response.Redirect(string.Format("~/Admin/ServiceReportView.aspx?jobID={0}", id));
        }

        protected void grvJobs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvJobs.DataSource = Job.GetAllUnarchived();
            grvJobs.PageIndex = e.NewPageIndex;
            grvJobs.DataBind();

            ViewState["PAGEINDEX"] = e.NewPageIndex;
        }

        protected void grvJobs_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this job?')");
            }
        }

        protected void grvJobs_DeleteRecord(object sender, GridViewDeleteEventArgs e)
        {
            string temp = grvJobs.DataKeys[e.RowIndex].Value.ToString();

            if (!string.IsNullOrWhiteSpace(temp))
            {
                Guid id = new Guid();

                if (Guid.TryParse(temp, out id))
                {
                    if (id != Guid.Empty)
                    {
                        try
                        {
                            Job j = new Job();
                            j.ID = id;
                            j.Delete();
                        }
                        catch (Exception ex) { }
                    }

                    BindData();
                }
            }
        }
    }
}